<?php
/**
* Management package
* @package Management
*/
class MgmBoxes extends MgmRequest
{
	protected $actions = array( "draw" => "DrawTemplate",
								"delete" => "DeleteBox",
								"purge" => "PurgeBox",
								"list" => "ListBoxes",
								"get" => "GetBox",
								"get_cloud" => "GetCloud");

	private function read_box_data( $atom )
	{
		$atom_data = AtomCache::GetAtomData( $atom->GetId() );
		if ( $atom_data )
		{
			return array( "title" =>  $atom_data->GetTitle(),
							"type" => $atom->GetAtomType(),
							"tag_cloud" =>  $atom->GetTagCloud(),
							"atom_id" => $atom->GetId() );
		}
	}

	public function GetCloud()
	{
		$ret = array();
		foreach ( AtomCache::GetAtoms( array() ) as $atom )
			foreach ( $atom->GetTagCloud() as $tag )
				if ( !isset( $ret[$tag] ) )
					$ret[$tag] = $tag;
		$this->SetOutput("tags", $ret );
	}

	protected function DeleteBox()
	{
		$id = Core::PostVar("id", "", "string");

	}

	protected function PurgeBox()
	{
		$id = Core::PostVar("id", "", "string");
		$this->SetError("PURGE NOT IMPLEMENTED YET");
	}

	protected function GetBox()
	{
		$id = Core::PostVar("id", "", "string");
		$atom = AtomCache::GetAtom( $id );
		if ( $atom )
			$this->SetOutput( "atom_data", $this->read_box_data( $atom ) );
		else
			$this->SetError( I18N("Sorry but atmo '$id' is invalid.") );
	}

	protected function DrawTemplate()
	{
		$template = new HtmlTemplate("core:management_atoms",
							array(
								));
		$template->DrawHtml();
	}

	protected function ListBoxes()
	{
		$type = strtolower( Core::PostVar("type", "all", "string") );
		$return_list = array();
		$atoms = AtomCache::GetAtoms( Core::PostVar("tags", array(), "sequence") );
		foreach ( $atoms as $atom )
		{
			if ( ($type == "all") ||
			     ($type == strtolower($atom->GetAtomType()) ) )
					$return_list[$atom->GetId()] = $this->read_box_data( $atom );
		}
		$this->SetOutput("atoms",$return_list);
	}
}
?>
